﻿/*----------------------------------------------------------------
 * 版权所有 (c) 2025 HaiTangYunchi  保留所有权利
 * CLR版本：4.0.30319.42000
 * 公司名称：
 * 命名空间：SalaryCalculator.Views
 * 唯一标识：ab1c2880-d0b4-4c23-ae40-a0ab873fdfe3
 * 文件名：ManageEmployeesForm
 * 
 * 创建者：海棠云螭
 * 电子邮箱：haitangyunchi@126.com
 * 创建时间：2025/5/10 20:24:46
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SalaryCalculator
{
    public partial class ManageEmployeesForm : Form
    {
        private EmployeeRepository _repository;
        private List<Employee> _employees;

        public ManageEmployeesForm()
        {
            InitializeComponent();

            // 初始化数据访问对象
            _repository = new EmployeeRepository();

            // 初始化UI
            InitializeUI();

            // 加载员工数据
            LoadEmployees();
        }

        private void InitializeUI()
        {
            // 设置DataGridView样式
            dataGridViewEmployees.AutoGenerateColumns = false;

            // 清除现有列
            dataGridViewEmployees.Columns.Clear();

            // 添加列
            dataGridViewEmployees.Columns.Add("Name", "姓名");
            dataGridViewEmployees.Columns["Name"].DataPropertyName = "Name";

            dataGridViewEmployees.Columns.Add("HourlyRate", "小时工资");
            dataGridViewEmployees.Columns["HourlyRate"].DataPropertyName = "HourlyRate";
            dataGridViewEmployees.Columns["HourlyRate"].DefaultCellStyle.Format = "N2";

            dataGridViewEmployees.Columns.Add("IsActive", "在职状态");
            dataGridViewEmployees.Columns["IsActive"].DataPropertyName = "IsActive";

        }

        private void LoadEmployees()
        {
            try
            {
                _employees = _repository.GetAllEmployees(true);
                dataGridViewEmployees.DataSource = null; // 先清空
                dataGridViewEmployees.DataSource = _employees;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载员工数据失败: {ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var addForm = new AddEmployeeForm();
            if (addForm.ShowDialog() == DialogResult.OK)
            {
                var newEmployee = new Employee
                {
                    Name = addForm.EmployeeName,
                    HourlyRate = addForm.HourlyRate,
                    IsActive = true
                };

                _repository.AddEmployee(newEmployee);
                LoadEmployees();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            EditSelectedEmployee();
        }

        private void dataGridViewEmployees_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0) // 确保点击的是数据行而不是标题行
            {
                EditSelectedEmployee();
            }
        }

        private void EditSelectedEmployee()
        {
            if (dataGridViewEmployees.CurrentRow == null)
            {
                MessageBox.Show("请先选择要编辑的员工", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var employee = dataGridViewEmployees.CurrentRow.DataBoundItem as Employee;
            if (employee == null) return;

            // 创建编辑窗体并传入当前员工数据
            using (var editForm = new AddEmployeeForm(employee.Name, employee.HourlyRate))
            {
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    // 更新员工信息
                    employee.Name = editForm.EmployeeName;
                    employee.HourlyRate = editForm.HourlyRate;

                    // 保存到数据库
                    _repository.UpdateEmployee(employee);

                    // 刷新列表
                    LoadEmployees();

                    MessageBox.Show("员工信息已更新", "成功",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnToggleStatus_Click(object sender, EventArgs e)
        {
            if (dataGridViewEmployees.CurrentRow == null)
            {
                MessageBox.Show("请先选择员工", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var employee = dataGridViewEmployees.CurrentRow.DataBoundItem as Employee;
            if (employee == null) return;

            employee.IsActive = !employee.IsActive;
            _repository.UpdateEmployee(employee);
            LoadEmployees();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}